package com.igit.contact.action;

import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.dataDictionary.service.DataDictionaryService;
import com.gosophia.organizationManagement.entity.Organization;
import com.gosophia.organizationManagement.entity.Position;
import com.gosophia.organizationManagement.service.OrganizationManagementService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophiaigit.common.busi.service.IGroupUtilService;
import com.gosophiaigit.contact.busi.entity.Contact;
import com.gosophiaigit.contact.busi.entity.ContactGroup;
import com.gosophiaigit.contact.busi.entity.ContactPartTime;
import com.gosophiaigit.contact.busi.entity.PositionUser;
import com.gosophiaigit.contact.busi.service.IContactGroupService;
import com.gosophiaigit.contact.busi.service.IContactService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 联系人单表的操作
 * 
 * @author zhangdianliang
 * @since 2011-3-29 上午10:00:33
 */
@Controller("contactSingleOperateAction")
@Scope("prototype")
public class ContactSingleOperateAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	@Autowired
	private IContactGroupService contactGroupService;
	@Autowired
	private IContactService contactService;
	@Autowired
	private IGroupUtilService groupUtilService;
	/** 组织机构结构接口 */
	@Autowired
	private OrganizationManagementService organizationManagementService;
	/** 数据字典操作接口 */
	@Autowired
	private DataDictionaryService dataDictionaryService;

	/** 联系人实例 */
	private Contact contact;
	/** 联系人ID */
	private Long contactId;
	/** 用户姓名 */
	private String contactName;
	/** 政治面貌 */
	private List<DataValue> contactPoliticalStatusList;
	/** 职级 */
	private List<DataValue> contactJobClassList;
	/** 民族 */
	private List<DataValue> contactNationalList;
	/** 个人组 */
	private List<ContactGroup> personalGroupList;
	/** 联系人个人组ID */
	private Long contactGroupId;
	/** 职位ID */
	private Long positionId;
	/** 联系人状态 */
	private Long contactUseFlagState;
	/** 社会兼职信息实体 */
	private ContactPartTime contactPartTime;
	/** 联系人类型 */
	private Long contactType;
	/** 人员和职位关联实体 */
	private PositionUser positionUser;
	/** 职位实体 */
	private Position position;
	/** 组织机构实体 */
	private Organization organization;
	/** 机构下所有的职位 */
	private List<Position> positionList;

	/**
	 * 通过联系人ID查询
	 */
	public String getContactById() {
		/** 查询联系人 */
		contact = contactService.getContactById(contactId);
		contactPoliticalStatusList = dataDictionaryService
				.findEnabledDataValueByEntryId(1212L);// 政治面貌
		contactJobClassList = dataDictionaryService
				.findEnabledDataValueByEntryId(1211L);// 职级
		contactNationalList = dataDictionaryService
				.findEnabledDataValueByEntryId(1213L);// 民族
		/** 得到当前用户名称 */
		contactName = groupUtilService.getUserInfoName(contact
				.getUserInfoId());
		/** 得到社会兼职信息 */
		Set<ContactPartTime> setContactPartTime = contact.getContactPartTimes();
		if (setContactPartTime.size() > 0) {
			for (ContactPartTime s : setContactPartTime) {
				contactPartTime = s;
			}
		}
		/** 得到组织机构信息 */
		getPositionUserByUserinfoId();
		getPositionbyId();
		return SUCCESS;
	}

	/** 通过职位id查询职位实体 */
	private void getPositionbyId() {
		if (positionUser != null && positionUser.getPositionId() != null) {
			position = organizationManagementService
					.findPositionDetailBypositionId(positionUser
							.getPositionId());
			/** 通过职位获得职位所在机构 */
			organization = position.getOrganization();
			positionList = organization.getPositionList();
		}
	}

	/**
	 * 获得人员和职位关联实体.
	 */
	public void getPositionUserByUserinfoId() {
		positionUser = groupUtilService
				.getPositionUserByUserinfoId(contact.getUserInfoId());
	}

	/**
	 * 通过联系人id查询联系人启用禁用状态
	 */
	public String getContactStateById() {
		contact = contactService.getContactById(contactId);
		contactUseFlagState = contact.getContactUseFlag();
		return SUCCESS;
	}

	/**
	 * 取得添加联系人所需数据
	 * 
	 * @return
	 */
	public String addContactInput() {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		personalGroupList = contactGroupService.findGroupListByGroupType(userInfoId,userAccountId,ContactGroup.PRIVATE_GROUP);
		contactPoliticalStatusList = dataDictionaryService
				.findEnabledDataValueByEntryId(1212L);
		contactJobClassList = dataDictionaryService
				.findEnabledDataValueByEntryId(1211L);
		contactNationalList = dataDictionaryService
				.findEnabledDataValueByEntryId(1213L);
		return SUCCESS;
	}

	/**
	 * 增加联系人
	 * 
	 * @param contact联系人
	 * @param contactName联系人名称
	 * @return
	 */
	public String addContact() {
		contactService.addContact(contact, contactName,
				contactGroupId, positionId,contactPartTime);
		return null;
	}

	/**
	 * 删除联系人，联系人做逻辑删除，联系人账号做物理删除
	 * 
	 * @param contactId联系人ID
	 */
	public void delContact() {
		contactService.delContact(contactId);
	}

	/**
	 * 编辑联系人
	 */
	public String modifyContact() {
		contactService.modifyContact(contact, contactName,contactPartTime,positionUser);
		return null;
	}

	public Long getContactId() {
		return contactId;
	}

	public void setContactId(Long contactId) {
		this.contactId = contactId;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public String getContactName() {
		return contactName;
	}

	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

	public List<DataValue> getContactPoliticalStatusList() {
		return contactPoliticalStatusList;
	}

	public void setContactPoliticalStatusList(
			List<DataValue> contactPoliticalStatusList) {
		this.contactPoliticalStatusList = contactPoliticalStatusList;
	}

	public List<DataValue> getContactJobClassList() {
		return contactJobClassList;
	}

	public void setContactJobClassList(List<DataValue> contactJobClassList) {
		this.contactJobClassList = contactJobClassList;
	}

	public List<DataValue> getContactNationalList() {
		return contactNationalList;
	}

	public void setContactNationalList(List<DataValue> contactNationalList) {
		this.contactNationalList = contactNationalList;
	}

	public List<ContactGroup> getPersonalGroupList() {
		return personalGroupList;
	}

	public void setPersonalGroupList(List<ContactGroup> personalGroupList) {
		this.personalGroupList = personalGroupList;
	}

	public Long getContactGroupId() {
		return contactGroupId;
	}

	public void setContactGroupId(Long contactGroupId) {
		this.contactGroupId = contactGroupId;
	}

	public Long getContactUseFlagState() {
		return contactUseFlagState;
	}

	public void setContactUseFlagState(Long contactUseFlagState) {
		this.contactUseFlagState = contactUseFlagState;
	}

	public Long getPositionId() {
		return positionId;
	}

	public void setPositionId(Long positionId) {
		this.positionId = positionId;
	}

	public Long getContactType() {
		return contactType;
	}

	public void setContactType(Long contactType) {
		this.contact.setContactType(contactType);
	}

	public ContactPartTime getContactPartTime() {
		return contactPartTime;
	}

	public void setContactPartTime(ContactPartTime contactPartTime) {
		this.contactPartTime = contactPartTime;
	}

	public PositionUser getPositionUser() {
		return positionUser;
	}

	public void setPositionUser(PositionUser positionUser) {
		this.positionUser = positionUser;
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public List<Position> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<Position> positionList) {
		this.positionList = positionList;
	}

}
